package com.molyfun.parents.rest;

import android.util.Log;

import com.molyfun.parents.MFPApplication;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.util.Auth;

import org.json.JSONObject;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/25/16.
 */
public class QiniuSDK {

    static String bucketname = "molyfun-hello";
    String ACCESS_KEY = "DWUoDRDz9mYA70km3JwmnfxQoQB2Tx0W6XG1ZkGg";
    String SECRET_KEY = "IfMu9Ah-9S1KXm9WB7MvdGbdSH9tK-rALrf8ELud";
    public static String QINIU_URL_PREFIX = "http://media.molyfun.com/";
    public static String QINIU_URL_AVATAR_POSTFIX = "?imageView2/1/w/200/h/200";
    // !用于保证返回的图片不会比原图大
    public static String QINIU_IMAGE_URL_SIZE_LIMIT = "?imageMogr2/size-limit/1024k!";

    public static String QINIU_URL_VIDEO_POSTFIX = "vframe/png/offset/0/w/480/h/480";

    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

//    static{
//        if(PreferencesStorageUtil.getServerMode(MFPApplication.getInstance()) == PreferencesStorageUtil.ServerMode.OFFICAL){
//            bucketname = "molyfun-hello";
//        }else{
//            bucketname = "molyfun-hello-antipiratelink";
//            QINIU_URL_PREFIX = "http://antipiratelink-media.molyfun.com/";
//        }
//    }

    private UploadManager mUploadManager;

    public QiniuSDK(){
        //初始化七牛
//        Configuration config = new Configuration.Builder()
//                .chunkSize(256 * 1024)  //分片上传时，每片的大小。 默认256K
//                .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认512K

//                .connectTimeout(10) // 链接超时。默认10秒
//                .responseTimeout(60) // 服务器响应超时。默认60秒
//                .recorder(recorder)  // recorder分片上传时，已上传片记录器。默认null
//                .recorder(recorder, keyGen)  // keyGen 分片上传时，生成标识符，用于片记录器区分是那个文件的上传记录
//                .zone(Zone.zone0) // 设置区域，指定不同区域的上传域名、备用域名、备用IP。默认 Zone.zone0
//                .build();
//        UploadManager uploadManager = new UploadManager(config);

        mUploadManager = new UploadManager();
    }

    public void uploadFile(final String filePath, final String uploadPath, final OnFileUpLoadCallback onPicUpLoadCallback){
        System.out.println("filePath-->" + filePath + "uploadPath--->" + uploadPath);
        mUploadManager.put(filePath, uploadPath, auth.uploadToken(bucketname),
                new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject res) {
                        //res包含hash、key等信息，具体字段取决于上传策略的设置。
                        Log.i("qiniu", key + ",\r\n " + info + ",\r\n " + res);
                        if (info.isOK()) {
                            if(onPicUpLoadCallback != null){
                                onPicUpLoadCallback.onFileUpload(QINIU_URL_PREFIX + key);
                            }
                        } else {
                            if(onPicUpLoadCallback != null){
                                onPicUpLoadCallback.onFileUploadFailed(filePath, uploadPath);
                            }
                        }
//                        if(onPicUpLoadCallback != null){
//                            onPicUpLoadCallback.onFileUpload(QINIU_URL_PREFIX + key);
//                        }
                        //key--->mfp/topic/ic_20161025-161225
                        //info--->{ver:7.2.3,ResponseInfo:1477383145379868,status:200, reqId:yxsAAEBQe8oluIAU, xlog:s.ph;s.ph;PFDS;PFDS:5;PTFDM:4;s.gh;PFDS/613;s.gh;PFDS/613;s.gh/EOF;PFDS/620;AUTHPROXY_xs_out:4/620;PTFDM;PTFDS:3;rdb.g/no such key;DBD/404;v4.exist/Document not found;rs24_12.ins;qtbl.ins:1;mc.s;RS:2;rs.put:7;UP:53;UP:54, xvia:1.1 xingdxin152:4 (Cdn Cache Server V2.0), 1.1 xiandxin21:6 (Cdn Cache Server V2.0), host:upload.qiniu.com, path:/mkfile/771526/mimeType/YXBwbGljYXRpb24vb2N0ZXQtc3RyZWFt/fname/aWNvbjIwMTYxMDI1LTE2MTIwNA==/key/bWZwL3RvcGljL2ljXzIwMTYxMDI1LTE2MTIyNQ==, ip:/219.144.69.180:80, port:80, duration:261.000000 s, time:1477383150, sent:0,error:null}
                        //res--->{"hash":"Fitu_e5ZSI_qjdsro99-yg8tQnDk","key":"mfp\/topic\/ic_20161025-161225"}

                    }
                }, null);
    }

    public interface OnFileUpLoadCallback{
        public void onFileUpload(String url);
        public void onFileUploadFailed(String path, String UploadPath);
    }

    public static String getBigPostPix(){
        if(MFPApplication.sScreenWidth <= 0){
            return "";
        }
        if(MFPApplication.sScreenWidth <= 320){
            return "&imageView2/1/w/150/h/150";
        }else if(MFPApplication.sScreenWidth > 320 && MFPApplication.sScreenWidth <= 480){
            return "&imageView2/1/w/180/h/180";
        }else if(MFPApplication.sScreenWidth > 480 && MFPApplication.sScreenWidth <= 540){
            return "&imageView2/1/w/200/h/200";
        }else if(MFPApplication.sScreenWidth > 540 && MFPApplication.sScreenWidth <= 800){
            return "&imageView2/1/w/480/h/480";
        }else if(MFPApplication.sScreenWidth > 800 && MFPApplication.sScreenWidth <= 1200){
            return "&imageView2/1/w/800/h/800";
        }else{
            return "&imageView2/1/w/1200/h/1200";
        }
    }

    public static String getBigUrl(String url){
        if(url.contains("clouddn") || url.contains("media.molyfun.com")){
            //七牛数据
            if(url.contains("vframe") || url.contains("thumbnail") || url.contains("imageView2") || url.contains("imageMogr2")){
                return url;
            }else{
                return url + getBigPostPix();
            }
        }else{
            return url;
        }
    }

    private static String getSmallPostPix(){
        if(MFPApplication.sScreenWidth <= 0){
            return "";
        }
        if(MFPApplication.sScreenWidth <= 320){
            return "&imageView2/1/w/100/h/100";
        }else if(MFPApplication.sScreenWidth > 320 && MFPApplication.sScreenWidth <= 480){
            return "&imageView2/1/w/100/h/100";
        }else if(MFPApplication.sScreenWidth > 480 && MFPApplication.sScreenWidth <= 800){
            return "&imageView2/1/w/160/h/160";
        }else if(MFPApplication.sScreenWidth > 800 && MFPApplication.sScreenWidth <= 1200){
            return "&imageView2/1/w/300/h/300";
        }else{
            return "&imageView2/1/w/400/h/400";
        }
    }

    public static String getSmallUrl(String url){
        if(url.contains("clouddn") || url.contains("media.molyfun.com")){
            //七牛数据
            if(url.contains("vframe") || url.contains("thumbnail") || url.contains("imageView2") || url.contains("imageMogr2")){
                return url;
            }else{
                return url + getSmallPostPix();
            }
        }else{
            return url;
        }
    }


    public static String getBaseVideoUrl(String url){
        if(url.contains("clouddn")){
            return url + "?avvod/m3u8/s/480x480";//"?avvod/s/480x480";
        }
        return url;
    }

}
